pub mod basic_op {
    use crate::config::app::App;
    use crate::db as DB;
    use crate::entity::http::{Empty, Resp};
    use crate::layers::op_auth::AuthOpChecker;
    use axum::{
        http::{header::CONTENT_TYPE, HeaderMap, HeaderValue, StatusCode},
        response::IntoResponse,
        Extension, Json,
    };
    use sqlx::{mysql::MySql, Pool};

    use crate::entity::mock_router::MockRouter;

    pub async fn get_routes(Extension(pool): Extension<Pool<MySql>>,app :Extension<App>,auth_checker:AuthOpChecker) -> impl IntoResponse {
        let mut headers = HeaderMap::new();
        if let Err(e) = auth_checker.check(&app) {
            return (StatusCode::UNAUTHORIZED,headers,Resp::exception(e.to_string(), Empty {}).to_string());
        }
        match DB::get_mock_routers(pool).await {
            Ok(rows) => {
                headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
                let content = serde_json::json!(rows);
                return (StatusCode::OK, headers, Resp::success(content).to_string());
            }
            Err(e) => (StatusCode::INTERNAL_SERVER_ERROR,headers,Resp::exception(e.to_string(), Empty {}).to_string()),
        }
    }

    pub async fn del_route(
        Extension(pool): Extension<Pool<MySql>>,
        Extension(app) :Extension<App>,
        auth_checker:AuthOpChecker,
        Json(data): Json<MockRouter>,
    ) -> impl IntoResponse {
        let mut headers = HeaderMap::new();
        if let Err(e) = auth_checker.check(&app) {
            return (StatusCode::UNAUTHORIZED,headers,Resp::exception(e.to_string(), Empty {}).to_string());
        }
        if let Err(e) = data.check_delete() {
            headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
            return (StatusCode::OK,headers,Resp::exception(e.to_string(), Empty {}).to_string(),);
        }
        match DB::del_route(pool, data.id).await {
            Ok(res) => {
                headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
                (StatusCode::OK, headers, Resp::success(res).to_string())
            }
            Err(e) => (StatusCode::INTERNAL_SERVER_ERROR,headers,e.to_string().clone()),
        }
    }

    pub async fn add_route(
        Extension(pool): Extension<Pool<MySql>>,
        Extension(app) :Extension<App>,
        auth_checker:AuthOpChecker,
        Json(mut data): Json<MockRouter>
    ) -> impl IntoResponse {
        let mut headers = HeaderMap::new();

        if let Err(e) = auth_checker.check(&app) {
            return (StatusCode::UNAUTHORIZED,headers,Resp::exception(e.to_string(), Empty {}).to_string());
        }
        if let Err(e) = data.check_insert() {
            headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
            return (StatusCode::OK,headers,Resp::exception(e.to_string(), Empty {}).to_string());
        }
        match DB::add_route(pool, data).await {
            Ok(res) => {
                headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
                (StatusCode::OK, headers, Resp::success(res).to_string())
            }
            Err(e) => (StatusCode::INTERNAL_SERVER_ERROR,headers,e.to_string().clone()),
        }
    }

    pub async fn update_route(
        Extension(pool): Extension<Pool<MySql>>,
        Extension(app) :Extension<App>,
        auth_checker:AuthOpChecker,
        Json(mut data): Json<MockRouter>
    ) -> impl IntoResponse {
        let mut headers = HeaderMap::new();
        if let Err(e) = auth_checker.check(&app) {
            return (StatusCode::UNAUTHORIZED,headers,Resp::exception(e.to_string(), Empty {}).to_string());
        }
        if let Err(e) = data.check_update() {
            headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
            return (StatusCode::OK,headers,Resp::exception(e.to_string(), Empty {}).to_string());
        }
        match DB::update_mock_router(pool, data).await {
            Ok(res) => {
                headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
                (StatusCode::OK, headers, Resp::success(res).to_string())
            }
            Err(e) => (StatusCode::INTERNAL_SERVER_ERROR,headers,e.to_string().clone()),
        }
    }
}

pub mod memory_op {
    use crate::{config::app::App, entity::http::{Empty, Resp}, layers::op_auth::AuthOpChecker, mock_routes};
    use axum::{http::StatusCode, response::IntoResponse, Extension};

    pub async fn fresh_route_data(auth_checker:AuthOpChecker,Extension(app):Extension<App>) -> impl IntoResponse {
        if let Err(e) = auth_checker.check(&app) {
            return (StatusCode::UNAUTHORIZED,Resp::exception(e.to_string(), Empty {}).to_string());
        }
        match mock_routes::ROUTERS.lock().await.update().await {
            Ok(()) => (StatusCode::OK, "success".to_string()),
            Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string().clone()),
        }
    }
}
